package com.wd.mqtt;

import com.sun.management.OperatingSystemMXBean;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {

    private static AtomicLong count = new AtomicLong(0);
    private static AtomicLong clientCount = new AtomicLong(0);
    private static OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

//    数量：6200耗时：114内存占用：2812.06640625
//    数量：6400耗时：120内存占用：2515.00390625
//    数量：6600耗时：126内存占用：2616.359375
//    数量：6800耗时：131内存占用：2714.33984375
//    数量：7000耗时：136内存占用：2805.26171875
//    数量：7200耗时：142内存占用：2905.9453125
// MICA
//    数量：3200耗时：38内存占用：3103.66796875
//    数量：3400耗时：45内存占用：3361.31640625
//    数量：3600耗时：51内存占用：3587.1640625
//    数量：3800耗时：58内存占用：3761.07421875

    public static void main(String[] args) throws MqttException, InterruptedException {
        double startFreeMemorySize = osmxb.getFreeMemorySize();

        MqttConfig mqttConfig = new MqttConfig();
        mqttConfig.setClientNum(1);
        mqttConfig.setClientName("test_");
        mqttConfig.setQos(0);
        mqttConfig.setAddress("mqtt://127.0.0.1:1883");
        List<MqttClientApi> clients = new ArrayList<>();
        CountDownLatch latch = new CountDownLatch(mqttConfig.getClientNum());
        long start = System.currentTimeMillis();

        ExecutorService executorService = Executors.newFixedThreadPool(200);
        for (int i = 0; i < mqttConfig.getClientNum(); i++) {
            int finalI = i;

////            MqttClientApi mqttClientApi =  new VertxmqttImpl(mqttConfig.getAddress());
//            MqttClientApi mqttClientApi =  new VertxmqttImpl(mqttConfig.getAddress());
//
//            mqttClientApi.conect("clientId"+ finalI);
////            long value = clientCount.incrementAndGet();
////            System.out.println(i);
//            clients.add(mqttClientApi);
//            long time =  System.currentTimeMillis() - start;
            MqttClientApi mqttClientApi =  new MicamqttImpl(mqttConfig.getAddress());
            clients.add(mqttClientApi);

            executorService.submit(() -> {


                mqttClientApi.conect("clientId"+ finalI);
                long value = clientCount.incrementAndGet();


                latch.countDown();
                if (value%1000==0){
                    long time =  System.currentTimeMillis() - start;
                    double nowFreeMemorySize = osmxb.getFreeMemorySize();
                    System.out.println("数量："+value+"耗时："+ time/1000 +"内存占用："+ (startFreeMemorySize-nowFreeMemorySize)/1024/1024);
                }
            });

//            System.out.println(i);

        }
        latch.await();

        long lastCount = 0;
//        while (true){
//            for (MqttClient client : clients) {
//                Thread.ofVirtual().start(new Runnable() {
//                    @Override
//                    public void run() {
//                        String payload = "Temperature: " + (Math.random() * 50) + "°C, Humidity: " + (Math.random() * 100) + "%";
//                        MqttMessage message = new MqttMessage(payload.getBytes());
//                        message.setQos(mqttConfig.getQos());
//                        try {
//                            client.publish("test", message);
//                        } catch (MqttException e) {
//                            throw new RuntimeException(e);
//                        }
//                        count.incrementAndGet();
//
//                    }
//                });
////                Thread.sleep(1);
//            }
//
//            long value = count.get();
//            if (value - lastCount > 10000){
//                long time =  System.currentTimeMillis() - start;
//                System.out.println("发送数量：{}"+value +"耗时： " + time/1000);
//                lastCount = value;
//            }
//
//        }

    }
}